// pages/information-detail/index.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    visible: false,
    jobList:[],
    cityList:[],
    companyList:[],
    companyName:'',
    name:'',
    phone:'',
    nowCompany:'',
    job:'',
    showJob:'',
    city:'',
    workTime:'',
    area:'',
    detail:{
      name:'',
      phone:'',
      bestDomain:'',
      company:'',
      roleName:'',
      companyName:'',
      roleFullName:'',
      city:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 当编辑个人资料inut框获取默认数据
    util.post(app.api.getByUnionid,{unionid:app.globalData.unionid},(res)=>{
      if(res.name){
        console.log(res)
        this.setData({
          detail : res
        })
      }
    })
    //
    var that = this
    let promiseCount = 0
    let domain = 0
    let info = wx.getStorageSync('info')
    if (info) {
      if(info.trade==="医药"){
        domain = 2
      }else{
        domain = 1
      }
    }
    // 获取工作列表
    util.post(app.api.getJobs,{i:domain},(res)=>{
      that.setData({
        jobList:res.data,
      },()=>{
        promiseCount++
        if(promiseCount == 3){
          that.setData({
            visible:true,
          })
        }
      })
    })
    // 获取城市列表
    util.post(app.api.getCities,{},(res)=>{
      that.setData({
        cityList:res.data,
      },()=>{
        promiseCount++
        if(promiseCount == 3){
          that.setData({
            visible:true,
          })
        }
      })
    })
    //获取公司列表
    util.post(app.api.getCompanys,{i:domain},(res)=>{
      that.setData({
        companyList:res.data,
      },()=>{
        promiseCount++
        if(promiseCount == 3){
          that.setData({
            visible:true,
          })
        }
      })
    })
  },
  saveJob: function(e){
    this.setData({
      job : e.detail.name
    })
  },
  saveCity: function(e){
    this.setData({
      city : e.detail.name
    })
  },
  saveCompany: function(e){
    this.setData({
      companyName : e.detail.name
    })
  },
  submit: function(e){
    // 
    let info1 = {}
    info1['job'] = this.data.job
    info1['city'] = this.data.city
    info1['company'] = this.data.companyName
    var obj = e.detail.value
    for(let item in obj){
      info1[item] = obj[item]
    }
    wx.setStorageSync('info1', info1)
    this.validate(info1)
  },
  validate: function(value){
    let goNextPage = true
    for(let item in value){
      if(!value[item]){
        goNextPage = false
      }
    }
    if(goNextPage){
      this.goNextPage()
    }else{
      this.showModal()
    }
  },
  // validate效验不通过，报错
  showModal(error) {
    wx.showModal({
      content: '请输入必填内容',
      showCancel: false,
    })
  },
  // 跳转下一页
  goNextPage: function(e){
    wx.navigateTo({
      url: '/pages/information-intention/index',
    })
  },
  //分享
  onShareAppMessage: function (res) {
    var that = this
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    var title = that.data.positionInfo.companyName + ' 招聘 ' + that.data.positionInfo.name
    return {
      // title: title,
      // path: 'pages/position-detail/position-detail?pid=' + that.data.positionInfo.id,
      // data: that.data.positionInfo,
      imageUrl: this.data.imgUrl,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },
  onShareTimeline: function (res) {
    var title = that.data.positionInfo.companyName + ' 招聘 ' + that.data.positionInfo.name
    return {
      title: title,
      query: 'pid=' + that.data.positionInfo.id,
    }
  },
})